package com.gancp.controller;

import com.gancp.Exception.BaseServiceException;
import com.gancp.common.response.ApiResult;
import com.gancp.common.util.UUIDUtil;
import com.gancp.entity.UserInfo;
import com.gancp.controller.base.BaseController;
import com.gancp.service.UserInfoService;
import com.gancp.vo.UserSessionVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author gancp
 * @Date 时间被黑洞吸走了
 * @Descripition 用户控制中心
 **/
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/modifyNickName")
    public ApiResult modifyNickName(@RequestBody UserInfo userInfo) {
        ApiResult apiResult = new ApiResult();
        String id = "";
        String userName = "";
        UserSessionVo userSessionVo = null;
        try {
            userSessionVo = getUserSessionVo();
            if (userSessionVo == null) {
                throw new BaseServiceException( "当前用户未登录" );
            }
            id = userInfo.getId();
            userName = userInfo.getNickname();
            if (StringUtils.isEmpty( id ) || StringUtils.isBlank( userName )) {
                throw new BaseServiceException( "用户信息不能为空" );
            }
            UserInfo dbUserinfo = userInfoService.selectByPrimaryKey( id );
            if (null == dbUserinfo) {
                throw new BaseServiceException( "对不起，用户信息不存在" );
            }
            dbUserinfo.setNickname( userName );
            userInfoService.updateByPrimaryKeySelective( dbUserinfo );
            apiResult.setMessage( "用户昵称修改成功" );
            apiResult.setResultData( dbUserinfo );
            log.info( "id【" + id + "】的用户将昵称修改为" + userName );
        } catch (Exception e) {
            if (e instanceof BaseServiceException) {
                apiResult.setFailureMessage( e.getMessage() );
            } else {
                apiResult.setFailureMessage( "用户信息修改失败" );
            }
            log.error( "失败：id【" + id + "】的用户将昵称修改为" + userName, e.getMessage() );
        }
        return apiResult;
    }


    @RequestMapping(value = "/registUser")
    public ApiResult registUser(@RequestBody UserInfo userInfo) {
        ApiResult apiResult = new ApiResult();
        String mobile = "";
        String id = "";
        try {

            if (userInfo == null) {
                throw new BaseServiceException( "用户信息不能为空" );
            }
            mobile = userInfo.getMobile();
            if (StringUtils.isBlank( mobile )) {
                throw new BaseServiceException( "用户信息不能为空" );
            }
            UserInfo dbUserinf = userInfoService.selectByMobile( mobile );
            if (dbUserinf != null) {
                throw new BaseServiceException( "该手机号已被注册" );
            }
            id = UUIDUtil.generate();
            userInfo.setId( id );
            userInfoService.insertSelective( userInfo );
            apiResult.setMessage( "用户注册成功" );
            log.info( "用户【" + id + "】注册成功" );
        } catch (Exception e) {
            if (e instanceof BaseServiceException) {
                apiResult.setFailureMessage( e.getMessage() );
            } else {
                apiResult.setFailureMessage( "用户注册失败" );
            }
            log.error( "用户注册失败", e.getMessage() );
        }
        return apiResult;
    }
}



